import React from 'react';
import {View, Image} from 'react-native';
import { Lt, Color, Label, Images, NavTheme as Nav, Jx, Flist, Fimage, Dialog, Jpp } from "../_utils";
import { Net, NET_NT, NET_ND } from "../_net"

class ViewMsg extends React.PureComponent{
    render(){
        const {source, uri, title, date, children} = this.props
        return <View style = {{backgroundColor:"white", flexDirection:"row", padding:Lt.pad, marginBottom:Lt.margin}}>
            {
                source ? <Image source = {source} /> : 
                <Fimage style={{width:40, height:40, borderRadius:20,}} uri={uri}/>
            }
            <View style = {{marginLeft:Lt.margin, flexShrink:1}}>
                <Label style = {{fontSize:Lt.c16, color:Color.c3}}>{title}</Label>
                {
                    date ? 
                    <Label style = {{color:Color.c9, marginVertical:5}}>{Jx.toDate(date)}</Label>
                    : null
                }
                {
                    Jx.isString(children) ?
                    <Label style = {{color:Color.c6}}>
                        {/* <Label style = {{opacity:0}}>缩进</Label> */}
                        {children}
                    </Label> : children
                }
            </View>
        </View>
    }
}

export class ApplyMsg extends React.PureComponent{
    constructor(props){
        super(props);
        this.onLoad = this.onLoad.bind(this);
        this.onItem = this.onItem.bind(this);
    }

    static navigationOptions = {
        tabBarIcon : ({focused, tintColor})=><Image source={focused ? Images.xiaoxi_2 : Images.xiaoxi}/>
    }

    onLoad(params){
        Net.request("advice/adv", params.pagination, {NET_NT, NET_ND,  params}).then(r=>{
            if(r.data){
                let arr = r.data.pageData;
                params.add(arr.length > 0 ? [arr[2]] : []);
            }else{
                params.add()
            }
        });
    }
    onItem(params){
        const item = params.item;
        return <ViewMsg title={item.title} source={Images.xiaoxi_da} date={item.createtime * 1000}>{item.content}</ViewMsg>
    }
    render(){
        return <View style = {{flex:1, backgroundColor:Color.back, }}>
            <Nav.StateBar />
            <View style={{...Nav.style, justifyContent:"center"}}>
                <Nav.TouchTitle title={"消息"}/>
            </View>
            <Flist
                onLoad={this.onLoad}
                onItem={this.onItem}
            />
        </View>;
    }
}
